package com.bjtech.web.action.management;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.ssi.web.result.XMLNode;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class ReportNode implements XMLNode {

	/**   */
	private static final long serialVersionUID = 1L;

	/** 节点名称 */
	private String name = null;

	/** 属性名—属性值 */
	private Map<String, String> attributes = new HashMap<String, String>();

	private List<ReportNode> children = new ArrayList<ReportNode>();

	public ReportNode() {
	}

	public ReportNode(String name) {
		this.name = name;
	}

	/**
	 * 
	 * @see com.epipe.adp.support.XMLNode#toXMLNode(org.w3c.dom.Document)
	 */
	public Node toXMLNode(Document document) {

		/** 生成节点主体 */
		Element element = document.createElement(name);

		/** 为节点添加attribute */
		Iterator<String> iter = attributes.keySet().iterator();
		String attribute = null;
		while (iter.hasNext()) {
			attribute = iter.next();
			element.setAttribute(attribute, attributes.get(attribute));
		}

		/** 为节点添加子节点 */
		Iterator<ReportNode> childIter = children.iterator();
		while (childIter.hasNext()) {
			element.appendChild(childIter.next().toXMLNode(document));
		}

		return element;
	}

	/**
	 * 进行默认外观的设置。只能用在chart节点。<br/>
	 * caption="Monthly Unit Sales" xAxisName="Month" yAxisName="Units" 
	 * decimalPrecision="0" formatNumberScale="0">
	 */
	public void applyDefaultRenderView() {
//		this.setAttribute("caption", "楼栋耗热量分析");
//		this.setAttribute("xAxisName", "建筑物");
//		this.setAttribute("yAxisName", "耗热量");
//		this.setAttribute("decimalPrecision", "0");
//		this.setAttribute("formatNumberScale", "0");
	}

	/**
	 * 获取节点某一属性的值
	 * 
	 * @param attName
	 *            属性名
	 * @return
	 */
	public String getAttribute(String attName) {
		return attributes.get(attName);
	}

	/**
	 * 为节点设置属性
	 * 
	 * @param attName
	 *            属性名
	 * @param attValue
	 *            属性值
	 */
	public void setAttribute(String attName, String attValue) {
		attributes.put(attName, attValue);
	}

	/**
	 * 添加子节点
	 * 
	 * @param child
	 */
	public void addChild(ReportNode child) {
		children.add(child);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Map<String, String> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, String> attributes) {
		this.attributes = attributes;
	}

	public List<ReportNode> getChildren() {
		return children;
	}

	public void setChildren(List<ReportNode> children) {
		this.children = children;
	}
}
